package com.sky.sky_server.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.sky_common.enumeration.OperationType;
import com.sky.sky_pojo.dto.CategoryDTO;
import com.sky.sky_pojo.entity.Category;
import com.sky.sky_pojo.entity.CategoryQueryPage;
import com.sky.sky_pojo.entity.PageResult;
import com.sky.sky_server.annotation.AutoFill;
import com.sky.sky_server.mapper.CategoryMapper;
import com.sky.sky_server.mapper.DishMapper;
import com.sky.sky_server.mapper.SetmealMapper;
import com.sky.sky_server.service.CategoryService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询
     */
    @Override
    public PageResult<Category> page(CategoryQueryPage categoryQueryPage) {
        PageHelper.startPage(categoryQueryPage.getPage(), categoryQueryPage.getPageSize());

        List<Category> categories = categoryMapper.page(categoryQueryPage);
        Page<Category> page = (Page<Category>) categories;

        List<Category> res = page.getResult();
        Long total = page.getTotal();
        page.close();

        return new PageResult<Category>(total, res);
    }

    /**
     * 启用，禁用分类
     */
    @Override
    @AutoFill(OperationType.UPDATE)
    public int updateStatus(Integer status, Long id) {
        Category category = Category.builder().status(status).id(id).build();
        // 获取当前时间
        // category.setUpdateTime(LocalDateTime.now());

        // 获取当前用户id
        // Long empId = BaseContext.getCurrentId();
        // category.setUpdateUser(empId);

        int res = categoryMapper.update(category);
        return res;
    }

    /**
     * 新增分类
     */
    @Override
    public int save(CategoryDTO categoryDTO) {
        Category category = Category.builder().build();
        BeanUtils.copyProperties(categoryDTO, category);

        // 设置默认值
        // category.setCreateTime(LocalDateTime.now());
        // category.setUpdateTime(LocalDateTime.now());

        //  设置操作人
        // Long empId = BaseContext.getCurrentId();
        // category.setCreateUser(empId);
        // category.setUpdateUser(empId);

        // 设置默认值
        category.setStatus(0);

        int res = categoryMapper.insert(category);

        return res;
    }

    @Override
    public int delete(Long id) {
        int dishCount = dishMapper.countByCategoryId(id);
        int setmealCount = setmealMapper.countByCategoryId(id);
        if(dishCount > 0 || setmealCount > 0){
            return -1;
        }
        return categoryMapper.deleteById(id);
    }

    /**
     * 修改分类排序状态
     */
    @Override
    public int updateCategorySort(CategoryDTO categoryDTO) {
        Category category = Category.builder().build();
        BeanUtils.copyProperties(categoryDTO, category);
        log.info("CategoryDTO:{}",categoryDTO);
        //当前时间
        // category.setUpdateTime(LocalDateTime.now());
        // 操作人
        // Long empId = BaseContext.getCurrentId();
        // category.setUpdateUser(empId);

        log.info("修改分类排序状态:{}",category);

        int res = categoryMapper.update(category);
        return res;
    }

    /**
     * 根据类型查询分类
     */
    @Override
    public List<Category> list(Integer type) {
        List<Category> res = null;
        if(type == null)
            res = categoryMapper.getAllCategory();
        res = categoryMapper.selectByType(type);
        return res.stream().sorted((a,b)->{
            return a.getSort() - b.getSort();
        }).collect(Collectors.toList());
    }
}
